<template>
  <div class="container">
    <h3 class="title">
      <svg-icon icon-class="list" class="title-icon" />广告管理
    </h3>
    <el-card>
      <div slot="header">
        <span>广告管理</span>
      </div>
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="广告名称">
          <el-input v-model="formInline.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="formInline.type" placeholder="请选择">
            <el-option label="请选择" value></el-option>
            <el-option label="全屏广告" value="0"></el-option>
            <el-option label="半屏广告" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="date"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleSearch">查询</el-button>
          <el-button type="primary" size="small" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
      <div style="margin-bottom:10px;">
        <el-table
          :data="tableData"
          height="calc(100vh - 420px)"
          border
          stripe
          style="width: 100%;margin-bottom:10px;"
        >
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="name" label="广告名称"></el-table-column>
          <el-table-column label="广告封面">
            <template slot-scope="scope">
              <img
                :src="getUrl(item)"
                v-for="(item,index) in scope.row.img.split(',')"
                width="60"
                height="40"
                alt
                style="margin:5px"
              />
            </template>
          </el-table-column>
          <el-table-column label="开始时间">
            <template slot-scope="scope">{{scope.row.startTime|getTime}}</template>
          </el-table-column>
          <el-table-column label="结束时间">
            <template slot-scope="scope">{{scope.row.endTime|getTime}}</template>
          </el-table-column>
          <el-table-column prop="projectNames" label="有效楼盘"></el-table-column>
          <el-table-column label="类型">
            <template slot-scope="scope">{{scope.row.type==1?'半屏广告':"全屏广告"}}</template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">{{scope.row.status==1?'上架':"下架"}}</template>
          </el-table-column>
          <el-table-column width="150" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDetail(scope.row.id)">详情</el-button>
              <el-button type="text" style="color:red" @click="handleEdit(scope.row.id)">编辑</el-button>
              <el-button
                type="text"
                style="color:red"
                v-if="scope.row.status==1"
                @click="setDowns(scope.row.id)"
              >下架</el-button>
              <el-button
                type="text"
                style="color:#1890ff"
                v-if="scope.row.status==0"
                @click="setUps(scope.row.id)"
              >上架</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
          style="float: right;"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getList, getExport, setUp, setDown } from "@/api/ad";
export default {
  name: "Ad",
  filters: {
    getTime(timestamp) {
      var time = new Date(timestamp);
      var y = time.getFullYear(); //getFullYear方法以四位数字返回年份
      var M = time.getMonth() + 1; // getMonth方法从 Date 对象返回月份 (0 ~ 11)，返回结果需要手动加一
      M = m < 10 ? "0" + M : M;
      var d = time.getDate(); // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
      d = d < 10 ? "0" + d : d;
      var h = time.getHours(); // getHours方法返回 Date 对象的小时 (0 ~ 23)
      h = h < 10 ? "0" + h : h;
      var m = time.getMinutes(); // getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
      m = m < 10 ? "0" + m : m;
      var s = time.getSeconds(); // getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
      s = s < 10 ? "0" + s : s;
      return y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
    }
  },
  data() {
    return {
      formInline: {
        date: "",
        name: "",
        status: "",
        type: ""
      },
      date: "",
      options: [],
      page: 1,
      pageSize: 10,
      totalPage: 0,
      tableData: [],
      startTime: "",
      endTime: ""
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    handleSearch() {
      this.page = 1;
      this.getData();
    },
    setUps(id) {
      this.$confirm("您确定要上架吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          setUp(id).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "上架成功",
                type: "success"
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下架"
          });
        });
    },
    setDowns(id) {
      this.$confirm("您确定要下架吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          setDown(id).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "下架成功",
                type: "success"
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下架"
          });
        });
    },
    getOptions() {
      getProject().then(res => {
        this.options = res.data;
      });
    },
    handleDetail(id) {
      this.$router.push({ path: "/ad/detail", query: { id: id } });
    },
    getUrl(img) {
      return process.env.VUE_APP_BASE_API + img;
    },
    getData() {
      console.log(this.date);
      if (this.date) {
        this.startTime = this.date[0];
        this.endTime = this.date[1];
      } else {
        this.startTime = "";
        this.endTime = "";
      }
      getList(
        this.startTime,
        this.endTime,
        this.formInline.name,
        this.page,
        this.pageSize,
        this.formInline.status,
        this.formInline.type
      ).then(res => {
        this.tableData = res.data;
        this.totalPage = res.recordsTotal;
      });
    },
    handleEdit(id) {
      this.$router.push({ path: "/ad/edit", query: { id: id } });
    },
    add() {
      this.$router.push({
        path: "/ad/edit"
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  background-color: rgb(240, 242, 245);
  position: relative;
  .title {
    font-size: 14px;
    font-weight: 400;
    .title-icon {
      font-size: 14px;
      display: inline-block;
      margin-right: 5px;
    }
  }
  /deep/ .el-card__header {
    padding: 18px 0;
    margin: 0 20px;
  }
}
.form-inline {
  border-bottom: 1px solid #e6ebf5;
  margin-bottom: 10px;
}
</style>
